import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    OnInit
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    RequirementMaterialListDto,
    MaterialChargeServiceProxy,
    PipelineNodesServiceProxy,
    InventoryOfMaterialListDto,
    ReserveMaterialOutput,
    MaterialForProduct,
    PipelineServiceProxy,
    TechDocMaterialEntity
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent, Paginator } from 'primeng/primeng';
import { Table } from 'primeng/components/table/table';

@Component({
    selector: 'yl2',
    templateUrl: './yl2.component.html',
    styleUrls: ['./yl2.component.less'],
    encapsulation: ViewEncapsulation.None
})
export class YL2ModalComponent extends AppComponentBase implements OnInit {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    bills = new Array<RequirementMaterialListDto>();
    subTaskCode = '';
    drawingCode = '';
    materials = new Array<TechDocMaterialEntity>();
    saving = false;

    filterText = '';

    pipe: PipelineListDto = new PipelineListDto();
    reserve = new Array<ReserveMaterialOutput>();
    charges = new Array<InventoryOfMaterialListDto>();

    outs = new Array<MaterialForProduct>();

    //table cols
    cols: any[];
    selectedColumns: any[];
    expandedRowKeys = [];

    constructor(
        injector: Injector,
        private _pipelineService: PipelineServiceProxy,
        private _materialChargeServiceProxy: MaterialChargeServiceProxy,
        private _pipelineNodeServiceProxy: PipelineNodesServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.cols = [
            {
                field: 'requestedTaskNum',
                header: '任务号',
                width: '150px',
                isDefaultSelect: true
            },
            {
                field: 'requestedDrawingNum',
                header: '图号',
                width: '120px',
                isDefaultSelect: true
            },
            {
                field: 'displayName',
                header: '名称',
                width: '100px',
                isDefaultSelect: true
            },
            { field: 'furnaceBatch', header: '炉批号', width: '100px' },
            {
                field: 'invCode',
                header: '质检编号',
                width: '100px',
                isDefaultSelect: true
            },
            {
                field: 'materialModel',
                header: '牌号',
                width: '100px',
                isDefaultSelect: true
            },
            {
                field: 'certificateNo',
                header: '合格证',
                width: '100px',
                isDefaultSelect: true
            },
            { field: 'materialCode', header: '物质编码', width: '100px' },
            { field: 'materialStatus', header: '材料状态', width: '100px' },
            { field: 'specification', header: '规格', width: '100px' },
            { field: 'size', header: '尺寸', width: '100px' },
            { field: 'unit', header: '计量单位', width: '100px' },
            { field: 'techCondition', header: '技术状态', width: '100px' }
        ];

        this.selectedColumns = this.cols.filter(el => {
            return el.isDefaultSelect;
        });
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;

        this._pipelineNodeServiceProxy
            .getRequirementBillByTask(pipe.id)
            .subscribe(result => {
                this.bills = result.requirementMaterials;
                this.subTaskCode = result.subTaskCode;
                this.drawingCode = result.drawingCode;
            });
        this._materialChargeServiceProxy
            .getReserveMaterial(this.pipe.id)
            .subscribe(result => {
                this.reserve = result;
            });
        this.getOuts();
    }
    getgy(event?: LazyLoadEvent){
        this._pipelineService.getTechDocDto(this.pipe.id).subscribe(result => {
            this.materials = result;
            console.log(this.materials);
        });
    }
    getOuts() {
        this._materialChargeServiceProxy
            .getOuts(this.pipe.id)
            .subscribe(result => {
                this.outs = result;
            });
    }

    getCharges(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._materialChargeServiceProxy
            .getMaterialCharges(
                this.filterText,
                1,
                51,
                'id desc',
                this.primengTableHelper.getSkipCount(this.paginator, event),
                5
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.charges = result.items;
                this.charges.forEach(el => {
                    this.expandedRowKeys[el.id] = 1;
                });
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());

        this.getOuts();
    }
}
